package com.example.onlinefood.controller;

import com.example.onlinefood.domain.Restaurant;
import com.example.onlinefood.mapper.RestaurantMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/v1/test")
public class TestDataController {
    private static final Logger logger = LoggerFactory.getLogger(TestDataController.class);

    @Resource
    private RestaurantMapper restaurantMapper;

    @GetMapping("/update-restaurant-data")
    public ResponseEntity<String> updateRestaurantData() {
        try {
            logger.info("开始更新餐厅数据");
            
            // 获取所有餐厅
            List<Restaurant> restaurants = restaurantMapper.findAll();
            
            // 为每个餐厅设置rating和distance值
            for (int i = 0; i < restaurants.size(); i++) {
                Restaurant restaurant = restaurants.get(i);
                // 设置一些模拟数据
                restaurant.setRating(4.0 + (i % 10) * 0.1); // 4.0-4.9的评分
                restaurant.setDistance(0.5 + i * 0.3); // 递增的距离
                
                logger.info("更新餐厅 {}: 评分={}, 距离={}", restaurant.getName(), restaurant.getRating(), restaurant.getDistance());
                
                // 更新餐厅数据
                int result = restaurantMapper.update(restaurant);
                logger.info("更新结果: {}", result > 0 ? "成功" : "失败");
            }
            
            logger.info("餐厅数据更新完成");
            return ResponseEntity.ok("餐厅数据已更新");
        } catch (Exception e) {
            logger.error("更新餐厅数据失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body("更新餐厅数据失败: " + e.getMessage());
        }
    }

    @GetMapping("/check-restaurant-data")
    public ResponseEntity<List<Restaurant>> checkRestaurantData() {
        try {
            logger.info("检查餐厅数据");
            List<Restaurant> restaurants = restaurantMapper.findAll();
            logger.info("共找到 {} 家餐厅", restaurants.size());
            return ResponseEntity.ok(restaurants);
        } catch (Exception e) {
            logger.error("检查餐厅数据失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).build();
        }
    }
}